<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { getCode } from '@/apis/user'
import 'element-plus/es/components/message/style/css'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user.js'

const userStore = useUserStore()

//route和router   带r是方法，不带是调用对象
const router = useRouter()


const form = ref({
  phone_number: '',
  phone_code: '',
  agree:true
})

const rules = {
  phone_number : [  //验证规则
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    //{ min: 3, max: 5, message: '用户名在3-5个字符内', trigger: 'blur' },
  ],
  phone_code : [
    { required: true, message: '验证码不能为空', trigger: 'blur' },
   // { min: 3, max: 12, message: '密码在3-12个字符内', trigger: 'blur' },
  ],
  agree : [
    //自定义校验规则
    {
      validator: (rule, value, callback) => {
        //console.log(value);
        //校验逻辑
        if (value === false) {
          callback(new Error('请先同意协议'))
        } else {
          callback()
        }
        //结果一致，写法不同
        //return value ? callback() : new Error('请先同意协议')
      }
    }
  ],
}

const secondsLeft = ref(60); 
const isDisabled = ref(false)
const onSubmitCode = ()=>{
  getCode(form.value.phone_number)
  .then(res=>{
    console.log(res);
    isDisabled.value = true;  
    const interval = setInterval(() => {  
      if (secondsLeft.value > 0) {  
        secondsLeft.value--;  
      } else {  
        clearInterval(interval);  
        isDisabled.value = false;  
        secondsLeft.value = 60; // 重置为60秒  
      }  
    }, 1000);  
  })
}
//获取form实例做统一校验 validate
const formRef = ref(null)

const sumbuit = ()=>{
  const {phone_number,phone_code} = form.value
  // 调用实例方法
  formRef.value.validate(async(valid)=>{
    // valid: 所有表单都通过校验  才为true
    console.log(valid);
    // 以valid做为判断条件 如果通过校验才执行登录逻辑
    if(valid){
      //const res = await login({phone_number,phone_code})
      await userStore.getUserInfo({phone_number,phone_code})
      //console.log(res);
      ElMessage({type:'success',message:'登录成功'})
        //跳转首页
        //router.push('/')
      router.replace('/') 
      
    }
  })
}
</script>
<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/" class="h1">安德美妆名品店</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账户登录</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form ref="formRef" :rules="rules" :model="form" label-position="right" label-width="80px" status-icon>
              <el-form-item  label="手机号" prop="phone_number">
                <el-input v-model="form.phone_number" placeholder="请输入手机号" />
              </el-form-item>
              <el-form-item label="验证码" class="flex justify-center" prop="phone_code">
                <el-input v-model="form.phone_code" placeholder="请输入验证码" class="verification"/>
                <el-button @click="onSubmitCode" :disabled="isDisabled" class="btn_code">
                  {{ isDisabled ? '获取验证码（' + secondsLeft + 's）' : '获取验证码' }}
                </el-button>
              </el-form-item>
              
              <el-form-item label-width="22px" prop="agree">
                <el-checkbox  size="large" v-model="form.agree">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn" @click="sumbuit">点击登录</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 安德美妆名品店</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang='scss'>
.verification {
  width: 45%;
  margin-right: 10px;
}
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 102px;
      width: 100%;
      font-size: 1.5em;
      //text-indent: -9999px;
      //background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
    .h1 {
      padding-top: 38px;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
.btn_code{
  width: 130px;
}
</style>